﻿using Dotnet.TopApi;
using Dotnet.TopApi.Request;
using HtmlAgilityPack;
using ScrapySharp.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Dotnet.Sui
{
    /// <summary>
    /// https://www.sui.com/tally/new.rmi  public interface ITopRequest<T> where T : TopResponse
    /// </summary>
    public class SuiRequest
    {
        public string Cookie { get; set; }
        public virtual IDictionary<string, string> GetParameters1()
        {
            TopDictionary parameters = new TopDictionary();
            parameters.AddHeadersPwd("Cookie", Cookie);
            return parameters;
        }
    }
    public class SuiRespose : TopResponse
    {

    }

    public class SuiDoResponse : TopResponse
    {
        protected List<Account> _cateAccount = new List<Account>();

        public List<Account> CateAccount { get => _cateAccount; }

        public virtual void ParseCateAccount(string html)
        {
            _cateAccount = BudgetIncomeCategory(html);
        }

        

        private List<Account> BudgetIncomeCategory(string html)
        {
            List<Account> list = new List<Account>();
            var doc = new HtmlDocument();
            doc.LoadHtml(html);
            var docNode = doc.DocumentNode;
            var firstZCNodes = docNode.CssSelect("div#list>div.list-msg").ToList();
            foreach (var node in firstZCNodes)
            {
                string id = node.Id.Replace("category", "").Trim('"');
                string name = node.CssSelect("li.li-level1").First().InnerText.Trim('"');
                string je = node.CssSelect("li.li-tj").First().InnerText.Trim('"');
                Console.WriteLine($"{id}  {name}  {je}");
                var secZCNodes = docNode.CssSelect($"div#listBox{id}>div.lb-row").ToList();
                List<Account> secZCList = new List<Account>();
                foreach (var secNode in secZCNodes)
                {
                    string secId = secNode.Id;
                    if (secId.StartsWith("addButton"))
                        continue;
                    secId = secId.Replace("category", "");
                    string secName = secNode.CssSelect("li.li-level2").First().InnerText.Trim();
                    string secJe = secNode.CssSelect("li.li-tj").First().InnerText.Trim();
                    secZCList.Add(new Account { Id = secId, Name = secName,  ChildAccount = null });
                    //Console.WriteLine($"   {secId}  {secName}  {secJe}");
                }
                list.Add(new Account() { Id = id, Name = name, ChildAccount = secZCList.Count > 0 ? secZCList : null }); ;
            }
            return list;
        }

        protected List<Account> StoreProjectCategory(string html)
        {
            List<Account> list = new List<Account>();
            var doc = new HtmlDocument();
            doc.LoadHtml(html);
            var docNode = doc.DocumentNode;
            var firstNodes = docNode.CssSelect("div#list-display>div.lb-row").ToList();
            foreach (var node in firstNodes)
            {
                string id = node.Id.Replace("category", "").Trim('"');
                string name = node.CssSelect("li.li-level2").First().InnerText.Trim('"');
                Console.WriteLine($"{id}  {name}  0");
                list.Add(new Account() { Id = id, Name = name,  ChildAccount = null });
            }
            return list;
        }
    }
}
